layui.define(['zlContext', 'layer', 'laytpl', 'colorpicker', 'zlPinYin', ], function (exports) {
    var $ = layui.$,
        zlContext = layui.zlContext,
        layer = layui.layer,
        colorpicker = layui.colorpicker,
        laytpl = layui.laytpl,
        zlPinYin = layui.zlPinYin,
        form = layui.form;

    var zlTheme = {
        ZL_theme: null,
        temporary_theme: null,
        defaulTheme: [{
            'theme_title': "黑色主题", // 主题名称
            'theme_title-py': 'HeiSeZhuTi',
            // logo
            'logoFontColorInput': 'rgba(255,255,255,.8)', // logo字体颜色
            'logoBgInput': '#20222A', // logo背景颜色
            // 导航栏
            'navFont': "Microsoft YaHei", // 导航栏字体
            'navBgInput': "#FFFFFF", // 导航栏背景
            'navFontColorInput': "#333333", // 导航栏 字体颜色
            'navIconColorInput': "#333333", // 导航栏 图标颜色
            // 左侧菜单
            'searchBoxBgInput': '#374850', // 搜索框背景
            'searchBoxFontColorInput': '#FFFFFF', // 搜索框字体色
            'menuBgInput': "#20222A", // 左侧菜单背景
            'childMenuBgInput': 'rgba(0, 0, 0, .3)',
            'menuFont': "Microsoft YaHei", // 左侧菜单字体
            'menuFontColorInput': "#FFFFFF", // 左侧菜单字体颜色
            'menuSelectedBgInput': '#277FD0', // 左侧菜单 选中项背景颜色
            'menuSelectedFontColorInput': '#FFFFFF', // 左侧菜单 选中项字体颜色
            'menuItemBorderColorInput': '无', // 左侧菜单 选中项字体颜色
            // 标准按钮
            'btnBgInput': "#277FD0", // 标准按钮背景
            'btnFont': "Microsoft YaHei", // 标准按钮字体
            'btnFontColorInput': "#FFFFFF", // 标准按钮字体颜色
            'btnIconColorInput': "#FFFFFF", // 标准按钮图标颜色
            // 警告按钮
            'dangerBtnBgInput': '#FF5722', // 警告按钮背景
            'dangerBtnFont': 'Microsoft YaHei', // 警告按钮字体
            'dangerBtnFontColorInput': "#FFFFFF", // 警告按钮字体颜色
            'dangerBtnIconColorInput': "#FFFFFF", // 警告按钮图标颜色
            // 开关按钮
            'switchBgInput': "#5FB878", // 开关按钮背景
            'switchFont': "Microsoft YaHei", // 开关按钮字体
            'switchFontColorInput': "#FFFFFF", // 开关按钮字体颜色
            // 复选框-默认
            'checkBoxBgInput': "#5FB878", // 复选框-默认 背景
            'checkBoxFontColorInput': "#FFFFFF", // 复选框-默认 字体颜色
            'checkboxFont': "Microsoft YaHei", // 复选框-默认 字体
            // 复选框-原始
            'originCheckBoxBgInput': "#5FB878", // 复选框-原始 背景
            // 'originCheckBoxFont': "", // 复选框-原始 字体
            'originCheckBoxFontColorInput': "#FFFFFF", // 复选框-原始 字体颜色
            // 单选框
            'radioBgInput': "#5FB878", // 单选框背景
            // 'radioFont': "", // 单选框字体
            // 'radioFontColorInput': "#fff", // 单选框字体颜色
            // 窗口
            'layerTitleBgInput': "#F8F8F8", // 窗口标题背景
            'layerCloseBtnColor': "1", // 窗口关闭按钮颜色
            'layerTitleFontColorInput': "#333333", // 窗口标题字体颜色

            'id': '25CE6A8B1C',
            // 'isActive': true, // 是否为选中主题
        }],
        formRender: function (data) {
            form.val("theme-form", {
                'theme_title': data.theme_title, // 主题名称
                'logoFontColorInput': data.logoFontColorInput, // logo字体颜色
                'logoBgInput': data.logoBgInput, // logo背景颜色
                'navFont': data.navFont, // 导航栏字体
                'navBgInput': data.navBgInput, // 导航栏背景
                'navFontColorInput': data.navFontColorInput, // 导航栏 字体颜色
                'navIconColorInput': data.navIconColorInput, // 导航栏 图标颜色
                'searchBoxBgInput': data.searchBoxBgInput, // 搜索框背景
                'searchBoxFontColorInput': data.searchBoxFontColorInput, // 搜索框字体色
                'menuBgInput': data.menuBgInput, // 左侧菜单背景
                'childMenuBgInput': data.childMenuBgInput, // 左侧子级菜单背景
                'menuFont': data.menuFont, // 左侧菜单字体
                'menuFontColorInput': data.menuFontColorInput, // 左侧菜单字体颜色
                'menuSelectedBgInput': data.menuSelectedBgInput, // 左侧菜单 选中项背景颜色
                'menuSelectedFontColorInput': data.menuSelectedFontColorInput, // 左侧菜单 选中项字体颜色
                'menuItemBorderColorInput': data.menuItemBorderColorInput, // 左侧菜单 菜单项边框
                'btnBgInput': data.btnBgInput, // 标准按钮背景
                'btnFont': data.btnFont, // 标准按钮字体
                'btnFontColorInput': data.btnFontColorInput, // 标准按钮字体颜色
                'btnIconColorInput': data.btnIconColorInput, // 标准按钮图标颜色
                'dangerBtnBgInput': data.dangerBtnBgInput, // 警告按钮背景
                'dangerBtnFont': data.dangerBtnFont, // 警告按钮字体
                'dangerBtnFontColorInput': data.dangerBtnFontColorInput, // 警告按钮字体颜色
                'dangerBtnIconColorInput': data.dangerBtnIconColorInput, // 警告按钮图标颜色
                'switchBgInput': data.switchBgInput, // 开关按钮背景
                'switchFont': data.switchFont, // 开关按钮字体
                'switchFontColorInput': data.switchFontColorInput, // 开关按钮字体颜色
                'checkBoxBgInput': data.checkBoxBgInput, // 复选框-默认 背景
                'checkBoxFontColorInput': data.checkBoxFontColorInput, // 复选框-默认 字体颜色
                'checkboxFont': data.checkboxFont, // 复选框-默认 字体
                'originCheckBoxBgInput': data.originCheckBoxBgInput, // 复选框-原始 背景
                'originCheckBoxFontColorInput': data.originCheckBoxFontColorInput, // 复选框-原始 字体颜色
                'radioBgInput': data.radioBgInput, // 单选框背景
                'layerTitleBgInput': data.layerTitleBgInput, // 窗口标题背景
                'layerCloseBtnColor': data.layerCloseBtnColor, // 窗口关闭按钮颜色
                'layerTitleFontColorInput': data.layerTitleFontColorInput, // 窗口标题字体颜色
            })
        },
        showform: function (allData, currNodeRwid, optid, v) {
            layer.open({
                type: 1,
                title: '系统主题选择',
                skin: 'layer-theme',
                area: ['100%', '100%'],
                id: 'theme-setter-layer',
                resize: false,
                btn: false,
                content: '<div class="themeSetter" id="theme-setter"></div>',
                success: function (layero, index) {
                    // 渲染弹窗内容
                    var getTpl = theme_setter_template.innerHTML,
                        view = document.getElementById('theme-setter');
                    laytpl(getTpl).render(allData, function (html) {
                        view.innerHTML = html;
                    });
                    if (!allData) return;

                    // 获取当前选中主题
                    for (var i = 0; i < allData.length; i++) {
                        if (allData[i].id == v) {
                            zlTheme.ZL_theme = allData[i];
                        }
                    }
                    layero.find('li[data-tid="' + zlTheme.ZL_theme.id + '"]').addClass('layui-this');
                    // 渲染选中主题内容到右侧form表单中
                    zlTheme.formRender(zlTheme.ZL_theme);
                    if (zlTheme.ZL_theme.id === '25CE6A8B1C') {
                        layero.find('input').prop('disabled', true);
                        layero.find('select').prop('disabled', true);
                        layero.find('.showFlag').find('input').prop('disabled', false);
                        layero.find('input[type="file"]').prop('disabled', false);
                        layero.find('.operationBtn').hide();
                    } else {
                        layero.find('input').attr("readOnly", "true");
                        layero.find('.showFlag').find('input').prop('readOnly', false);
                    }

                    // 颜色输入框关联到颜色选择器
                    layero.find('input').change(function () {
                        if ($(this).attr('id')) {
                            colorpicker.render({
                                elem: '#' + $(this).attr('id').replace(/Input/ig, ''),
                                color: $(this).val(),
                                format: 'rgb',
                                alpha: true
                            });
                        }
                    })

                    colorpickerRender();
                    form.render(null, 'theme-form');
                    optionsFontFamilyRender();

                    form.on('select', function (data) {
                        $(data.othis).find('input').css('font-family', data.value);
                        layero.find('button[lay-filter="themeForm"]').addClass('revised');
                        // if ($(data.elem).parents('div.layui-form-item').find('.showFlag').length > 0) {
                        //     var changeKey = $(data.elem).attr('name');
                        //     zlTheme.ZL_theme[changeKey] = data.value;
                        //     themeEffect(zlTheme.ZL_theme)
                        // }
                    });

                    layero.on('blur', 'input[name="theme_title"]', function () {
                        if ($(this).val() != zlTheme.ZL_theme.theme_title && !layero.find('li.layui-this').hasClass('newTheme')) {
                            layero.find('button[lay-filter="themeForm"]').addClass('revised');
                        }
                    })

                    themeEffect(zlTheme.ZL_theme);
                    // 新增主题
                    function addTheme(layero, that) {
                        var id = zlContext.generateId().toLocaleUpperCase();
                        var Existing_themes_num = parseInt(layero.find('ul.layadmin-setTheme-color').find('li').length) + 1,
                            theme_title_py = zlPinYin.A('新增主题-' + Existing_themes_num),
                            addHtml = [
                                '<li data-tid="' + id + '" data-alias="' + theme_title_py + '" class="layui-this newTheme"> ',
                                '    <div class="theme-name">新增主题-' + Existing_themes_num + '</div> ',
                                '    <div class="blank"></div> ',
                                '    <div class="layadmin-setTheme-header" style="background-color: #f5f5f5;"></div> ',
                                '    <div class="layadmin-setTheme-side" style="background-color: #20222A;"> ',
                                '        <div class="layadmin-setTheme-logo" style="background-color: #20222A;"></div> ',
                                '    </div> ',
                                '</li>'
                            ];
                        layero.find('li.layui-this').removeClass('layui-this');
                        zlTheme.formRender(zlTheme.defaulTheme[0]);
                        form.val("theme-form", {
                            'theme_title': '新增主题-' + Existing_themes_num
                        });

                        layero.find('input').prop({
                            'readOnly': true,
                            'disabled': false
                        });
                        layero.find('input[name="theme_title"]').prop({
                            'readOnly': false,
                            'disabled': false
                        });
                        layero.find('select').prop('disabled', false);
                        layero.find('.operationBtn').show();
                        colorpickerRender();
                        optionsFontFamilyRender();
                        layero.find('ul.layadmin-setTheme-color').append(addHtml.join(' '))
                    }
                    layero.on('click', '#addTheme', function () {
                        if (layero.find('li.newTheme').length > 0) {
                            zlContext.warnMsg('请保存当前新增主题成功后再操作！');
                            return;
                        }
                        if (layero.find('button[lay-filter="themeForm"]').hasClass('revised')) {
                            // var that = this;

                            layer.confirm('当前主题内容已被修改，是否保存？', {
                                title: false,
                                resize: false,
                                btn: ['保存', '取消'],
                                btnAlign: '',
                                anim: 1,
                                icon: 3
                            }, function (index) {
                                layero.find('button[lay-filter="themeForm"]').click();
                                addTheme(layero);
                                layer.close(index);
                            }, function () {
                                layero.find('button[lay-filter="themeForm"]').removeClass('revised');
                                addTheme(layero);
                            });
                            return;
                        }
                        addTheme(layero);
                    })
                    // 删除主题
                    layero.on('click', '#delTheme', function () {
                        var id = layero.find('li.layui-this').attr('data-tid');
                        if (id === '25CE6A8B1C') {
                            zlContext.warnMsg('系统自带主题无法删除！')
                        } else if (layero.find('li.layui-this').hasClass('newTheme')) {
                            layero.find('li.layui-this.newTheme').remove();
                            layero.find('.revised').removeClass('revised');
                            layero.find('.layui-card-theme').find('li:first-child').click();
                        } else {
                            layero.find('.revised').removeClass('revised');
                            for (var i = 0; i < allData.length; i++) {
                                if (allData[i].id == id) {
                                    zlTheme.temporary_theme = allData[i];
                                    allData.splice(i, 1);
                                    // 保存数据
                                    saveTheme('delete');
                                }
                            }
                        }
                    })
                    // 切换主题
                    function switchTheme(layero, that) {
                        layero.find('ul.layadmin-setTheme-color').find('li').removeClass('layui-this');
                        $(that).addClass('layui-this');
                        var id = $(that).attr('data-tid');
                        for (var i = 0; i < allData.length; i++) {
                            if (allData[i].id == id) {
                                zlTheme.formRender(allData[i]);
                                themeEffect(allData[i]);
                                zlTheme.ZL_theme = allData[i];
                                break;
                            }
                        }
                        colorpickerRender();
                        optionsFontFamilyRender();
                        // 默认主题禁止修改
                        if (id === '25CE6A8B1C') {
                            layero.find('input').prop('disabled', true);
                            layero.find('select').prop('disabled', true);
                            layero.find('.showFlag').find('input').prop('disabled', false);
                            layero.find('input[type="file"]').prop('disabled', false);
                            layero.find('.operationBtn').hide();
                        } else {
                            layero.find('input').prop({
                                'disabled': false,
                                'readonly': true
                            });
                            layero.find('select').prop('disabled', false);

                            layero.find('.showFlag').find('input').prop('readOnly', false);
                            layero.find('input[name="theme_title"]').prop('readOnly', false);
                            layero.find('.operationBtn').show();
                        }
                        form.render(null, 'theme-form');
                    }
                    layero.find('ul.layadmin-setTheme-color').on('click', 'li', function () {
                        if ($(this).hasClass('layui-this')) return;
                        if (layero.find('li.newTheme').length > 0) {
                            zlContext.warnMsg('请保存当前新增主题成功后再操作！');
                            return;
                        }
                        if (layero.find('button[lay-filter="themeForm"]').hasClass('revised')) {
                            var that = this;

                            layer.confirm('当前主题内容已被修改，是否保存？', {
                                title: false,
                                resize: false,
                                btn: ['保存', '取消'],
                                btnAlign: '',
                                anim: 1,
                                icon: 3
                            }, function (index) {
                                layero.find('button[lay-filter="themeForm"]').click();
                                switchTheme(layero, that);
                                layer.close(index);
                            }, function () {
                                switchTheme(layero, that);
                                layero.find('button[lay-filter="themeForm"]').removeClass('revised')
                            })
                            return;
                        }
                        switchTheme(layero, this)
                    })
                    // 导入json
                    layero.on('click', '#importTheme', function (e) {
                        var fileInput = layero.find('input[name="importThemeInput"]')[0];
                        fileInput.value = ''
                        if (layero.find('li.newTheme').length > 0 || layero.find('button[lay-filter="themeForm"]').hasClass('revised')) {
                            zlContext.warnMsg('请保存当前新增或修改的主题成功后再操作！');
                            return;
                        }
                        e.stopPropagation();
                        $(this).siblings('input').click();
                    })
                    layero.find('input[name="importThemeInput"]').change(function () {
                        if (window.File || window.FileReader || window.FileList || window.Blob) {
                            var selectedFile = this.files[0],
                                reader = new FileReader();
							/*
                            if (selectedFile.type != 'application/json') {
                                zlContext.warnMsg('请选择JSON格式的主题数据导入！');
                                return;
                            }
							*/	
                            reader.readAsText(selectedFile);

                            setTimeout(function () {
                                var importThemeData = JSON.parse(reader.result);
                                var id = zlContext.generateId().toLocaleUpperCase();
                                for (var i = 0; i < allData.length; i++) {
                                    if (importThemeData.theme_title == allData[i].theme_title) {
                                        importThemeData.theme_title = importThemeData.theme_title + '-副本';
                                        importThemeData['theme_title-py'] = zlPinYin.A(importThemeData.theme_title);
                                    }
                                }

                                var importHtml = [
                                    '<li data-tid="' + id + '" data-alias="' + importThemeData['theme_title-py'] + '" class="layui-this"> ',
                                    '    <div class="theme-name">' + importThemeData.theme_title + '</div> ',
                                    '    <div class="blank"></div> ',
                                    '    <div class="layadmin-setTheme-header" style="background-color: ' + importThemeData.navBgInput + ';"></div> ',
                                    '    <div class="layadmin-setTheme-side" style="background-color: ' + importThemeData.menuBgInput + ';"> ',
                                    '        <div class="layadmin-setTheme-logo" style="background-color: ' + importThemeData.logoBgInput + ';"></div> ',
                                    '    </div> ',
                                    '</li>'
                                ];

                                layero.find('li.layui-this').removeClass('layui-this');
                                layero.find('ul.layadmin-setTheme-color').append(importHtml.join(' '))

                                layero.find('input').prop({
                                    'disabled': false,
                                    'readonly': true
                                });
                                layero.find('select').prop('disabled', false);

                                layero.find('.showFlag').find('input').prop('readOnly', false);
                                layero.find('input[name="theme_title"]').prop('readOnly', false);
                                layero.find('.operationBtn').show();

                                zlTheme.formRender(importThemeData);
                                importThemeData.id = id;
                                zlTheme.temporary_theme = importThemeData;
                                zlTheme.ZL_theme = importThemeData;
                                allData = allData.concat(importThemeData)
                                colorpickerRender();
                                optionsFontFamilyRender();
                                saveTheme('import');
                            }, 0)

                        }
                    })
                    // 导出json
                    layero.on('click', '#exportTheme', function () {
                        if (layero.find('li.newTheme').length > 0 || layero.find('button[lay-filter="themeForm"]').hasClass('revised')) {
                            zlContext.warnMsg('请保存当前新增或修改的主题成功后再操作！');
                            return;
                        }
                        var exportThemeId = layero.find('li.layui-this').attr('data-tid'),
                            themeName = layero.find('li.layui-this').find('.theme-name').html();
                        for (var i = 0; i < allData.length; i++) {
                            if (allData[i].id == exportThemeId) {
                                var blob = new Blob([JSON.stringify(allData[i])], {
                                    type: ""
                                });
                                saveAs(blob, themeName + ".json");
                            }
                        }
                    })
                    // 新增或修改主题后保存
                    layero.on('click', '.operationBtn.saveData', function () {
                        layero.find('button[lay-filter="themeForm"]').click();
                    })
                    form.on('submit(themeForm)', function (data) {
                        if (!data.field.theme_title) {
                            zlContext.warnMsg('请输入主题名称!');
                            return false;
                        }
                        var theme_title_py = zlPinYin.A(data.field.theme_title),
                            id = layero.find('li.layui-this').attr('data-tid'),
                            flag = false,
                            index = null;
                        data.field['theme_title-py'] = theme_title_py;
                        data.field.id = id;
                        zlTheme.ZL_theme = data.field;
                        for (var i = 0; i < allData.length; i++) {
                            if (allData[i].theme_title == data.field.theme_title) {
                                flag = true;
                            }
                            if (id == allData[i].id) {
                                index = i;
                            }
                        }

                        if (layero.find('li.newTheme').length > 0) {
                            if (flag) {
                                zlContext.warnMsg('主题名称已存在，请更换！');
                                return false;
                            } else {
                                allData = allData.concat(data.field);
                                //保存数据
                                saveTheme('add', data.field);
                            }
                        } else {
                            allData[index] = data.field;
                            //保存数据
                            saveTheme('edit', data.field);
                        }

                        zlTheme.temporary_theme = data.field;
                        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
                    });
                    // 重置
                    layero.on('click', '.operationBtn.reset', function () {
                        layero.find('button[type="reset"]').click();
                    })
                    // 选择主题后确认
                    function selectedTheme(currNodeRwid, optid) {
                        var dataparams = {};
                        dataparams.ORGID = currNodeRwid;
                        dataparams.RWID = optid;
                        dataparams.TITLE = '系统主题设置';
                        dataparams.v = layero.find('li.layui-this').attr('data-tid');
                        zlContext.post(layui.cache['contentPath'] + '/manager/coresysopt/modify', dataparams, function (data) {
                            if (data.state == 'OK') {
                                zlContext.successMsg('保存成功!');
                                if (sessionStorage.getItem('zltheme') == null) {
                                    var userData = zlContext.getCurrentUser()
                                    if (currNodeRwid == userData.currentOrgId) {
                                        zlTheme.setTheme(zlTheme.ZL_theme);
                                    }
                                }
                                
                            } else {
                                zlContext.errorMsg(data.msg);
                            }
                        });
                    }
                    layero.on('click', '#themeConfirmBtn', function () {
                        if (layero.find('li.newTheme').length > 0 || layero.find('button[lay-filter="themeForm"]').hasClass('revised')) {
                            layer.confirm('当前主题内容已被修改，是否保存后选定此主题？', {
                                title: false,
                                resize: false,
                                btn: ['保存', '取消'],
                                btnAlign: '',
                                anim: 1,
                                icon: 3
                            }, function (index) {
                                layero.find('button[lay-filter="themeForm"]').click();
                                selectedTheme(currNodeRwid, optid)
                                layer.close(index);
                            }, function () {
                                layero.find('button[lay-filter="themeForm"]').removeClass('revised')
                                selectedTheme(currNodeRwid, optid)
                            });
                            return;
                        }
                        selectedTheme(currNodeRwid, optid);
                    })

                    function colorpickerRender() {
                        var colorRenderItems = layero.find('.colorpickerElem')
                        for (var index = 0; index < colorRenderItems.length; index++) {
                            var element = colorRenderItems[index],
                                inputVal = $('#' + $(element).attr('id') + 'Input').val();
                            colorpicker.render({
                                elem: '#' + $(element).attr('id'),
                                color: inputVal,
                                alpha: true,
                                format: 'rgb',
                                done: function (color) {
                                    if (layero.find('li.layui-this').attr('data-tid') == '25CE6A8B1C') {
                                        zlContext.warnMsg('系统自带主题禁止修改！');
                                        return false;
                                    }
                                    var inputVal = color != '' ? (color.indexOf('#') != -1 ? color.toLocaleUpperCase() : zlTheme.hexify(color).toLocaleUpperCase()) : (this.elem == "#menuItemBorderColor" ? '无' : '');

                                    $(this.elem + 'Input').val(inputVal);
                                    layero.find('button[lay-filter="themeForm"]').addClass('revised');
                                }
                            });
                        }
                    }

                    function optionsFontFamilyRender() {
                        var select_items = layero.find('select');
                        for (var i = 0; i < select_items.length; i++) {
                            var dd_items = $(select_items[i]).siblings('.layui-form-select').find('dd'),
                                input_item = $(select_items[i]).siblings('.layui-form-select').find('input');
                            input_item.css('font-family', input_item.val())
                            for (var k = 0; k < dd_items.length; k++) {
                                if ($(dd_items[k]).attr('lay-value')) {
                                    $(dd_items[k]).css('font-family', $(dd_items[k]).attr('lay-value'))
                                }
                            }
                        }
                    }

                    function themeEffect(data) {
                        layero.find('.standardButton').css({
                            'background-color': data.btnBgInput ? data.btnBgInput : 'rgb(39, 127, 208)',
                            'font-family': data.btnFont + '!important',
                            'color': data.btnFontColorInput ? data.btnFontColorInput : 'rgb(255, 255, 255)'
                        }).find('i').css('color', data.btnIconColorInput ? data.btnIconColorInput : 'rgb(255, 255, 255)');

                        layero.find('.dangerButton').css({
                            'background-color': data.dangerBtnBgInput ? data.dangerBtnBgInput : '#FF5722',
                            'font-family': data.dangerBtnFont + '!important',
                            'color': data.dangerBtnFontColorInput ? data.dangerBtnFontColorInput : 'rgb(255, 255, 255)'
                        }).find('i').css('color', data.dangerBtnIconColorInput ? data.dangerBtnIconColorInput : 'rgb(255, 255, 255)');

                        layero.find('.switchButton .layui-form-switch').css({
                            'border-color': data.switchBgInput ? data.switchBgInput : 'rgb(95, 184, 120)',
                            'background-color': data.switchBgInput ? data.switchBgInput : 'rgb(95, 184, 120)'
                        }).find('em').css({
                            'font-family': data.switchFont + '!important',
                            'color': data.switchFontColorInput ? data.switchFontColorInput + '!important' : 'rgb(255, 255, 255) !important'
                        });

                        layero.find('.defaultCheckBox').css('border-color', data.checkBoxBgInput ? data.checkBoxBgInput : 'rgb(95, 184, 120)')
                            .find('span').css({
                                'background-color': data.checkBoxBgInput ? data.checkBoxBgInput : 'rgb(95, 184, 120)',
                                'color': data.checkBoxFontColorInput ? data.checkBoxFontColorInput : 'rgb(255, 255, 255)',
                                'font-family': data.checkboxFont + '!important'
                            }).siblings('i').css('color', data.checkBoxBgInput ? data.checkBoxBgInput : 'rgb(95, 184, 120)');

                        layero.find('.originCheckBox i').css({
                            'border-color': data.originCheckBoxBgInput ? data.originCheckBoxBgInput : 'rgb(95, 184, 120)',
                            'background-color': data.originCheckBoxBgInput ? data.originCheckBoxBgInput : 'rgb(95, 184, 120)',
                            'color': data.originCheckBoxFontColorInput ? data.originCheckBoxFontColorInput : 'rgb(255, 255, 255)'
                        });

                        // layero.find('.layui-layer-title').css({
                        //     'background-color': data.layerTitleBgInput + '!important',
                        //     'color': data.layerTitleFontColorInput ? data.layerTitleFontColorInput : 'rgb(51, 51, 51)'
                        // }).siblings('span a').css('background-position', (data.layerCloseBtnColor ? data.layerCloseBtnColor : '1') +'px -40px');
                    }
                    //保存主题
                    function saveTheme(type, obj) {
                        var currentLi = layero.find('li.layui-this');
                        var txt = '',
                            errTxt = '',
                            url = layui.cache['contentPath'] + '/manager/coresysopt/modify';
                        var dataparams = {};
                        dataparams.ORGID = currNodeRwid;
                        dataparams.RWID = optid;
                        dataparams.TITLE ="系统主题设置--保存主题";
                        dataparams.XTYPE_CV = JSON.stringify(allData);
                        if (type == 'delete') {
                            txt = '删除成功';
                            errTxt = '删除失败';
                            url = layui.cache['contentPath'] + '/manager/coresysopt/removetheme';
                            var tempParams = {};
                            tempParams.entity = dataparams;
                            tempParams.id = currentLi.attr('data-tid');
                            dataparams = tempParams;
                        } else if (type == 'import') {
                            txt = '导入成功';
                            errTxt = '导入失败';
                        } else {
                            txt = '保存成功';
                            errTxt = '保存失败';
                        }

                        zlContext.post(url, dataparams, function (data) {
                            if (data.state == 'OK') {
                                zlContext.successMsg(txt);
                                if (type == 'add' || type == 'edit' || type == 'import') {
                                    currentLi.attr('data-alias', zlTheme.temporary_theme['theme_title-py'])
                                        .removeClass('newTheme')
                                        .find('.theme-name').html(zlTheme.temporary_theme.theme_title)
                                        .siblings('.layadmin-setTheme-header').css('background-color', zlTheme.temporary_theme.navBgInput)
                                        .siblings('.layadmin-setTheme-side').css('background-color', zlTheme.temporary_theme.menuBgInput)
                                        .find('.layadmin-setTheme-logo').css('background-color', zlTheme.temporary_theme.logoBgInput);

                                    zlTheme.formRender(zlTheme.ZL_theme);
                                    themeEffect(zlTheme.ZL_theme)
                                    layero.find('button[lay-filter="themeForm"]').removeClass('revised');
                                } else if (type == 'delete') {
                                    currentLi.prev().click();
                                    currentLi.remove();
                                }
                            } else {
                                if (type == 'add' || type == 'import') {
                                    for (var i = 0; i < allData.length; i++) {
                                        if (allData[i].id == zlTheme.temporary_theme.id) {
                                            allData.splice(i, 1)
                                        }
                                    }
                                    currentLi.prev().click();
                                    currentLi.remove();
                                    zlContext.errorMsg(errTxt);
                                } else if (type == 'delete') {
                                    allData = allData.concat(zlTheme.temporary_theme);
                                    zlContext.errorMsg(data.msg);
                                }
                            }
                        });
                    }
                }
            })
        },
        setTheme: function (data) {
            if (data.childMenuBgInput.indexOf('.') != -1 && data.childMenuBgInput.indexOf(',') != -1) {
                var arr = data.childMenuBgInput.split(',');
                arr.pop();
                data.childMenuTipsBgInput = arr.join() + ')';
            }
            data.layerMinBtnColor = data.layerCloseBtnColor == 1 ? "#2E2D3C" : "#fff";
            data.layerMaxBtnColor = data.layerCloseBtnColor == 1 ? "-32" : "-102";
            data.layerMaxMinBtnColor = data.layerCloseBtnColor == 1 ? "-65" : "-120";
            data.menuItemBorderColorInput = data.menuItemBorderColorInput == '无' ? 'none' : ('1px solid ' + data.menuItemBorderColorInput);
            var id = 'ZL_layadmin_theme',
                pid = 'ZL_layadmin_theme_p',
                style = document.createElement('style'),
                p_style = parent.document.createElement('style'),
                styleElem = document.getElementById(id),
                p_styleElem = parent.document.getElementById(pid),
                // iframe 子页面主题调整
                styleText = laytpl([
                    //主题色
                    '.layui-layer .layui-layer-title,' // 弹层标题背景颜色 待定
                    ,'.layui-layer-iframe .layui-layer-title'
                    , '{background-color:{{d.layerTitleBgInput}} !important;color:{{d.layerTitleFontColorInput}};}' //
                    , '.layui-layer-setwin .layui-layer-min cite' //
                    , '{background-color:{{d.layerMinBtnColor}};}' // 
                    , '.layui-layer-setwin .layui-layer-max' //
                    , '{background-position:{{d.layerMaxBtnColor}}px -40px;}' //
                    , '.layui-layer-setwin .layui-layer-max.layui-layer-maxmin' //
                    , '{background-position:{{d.layerMaxMinBtnColor}}px -40px;}', '.layui-layer-setwin .layui-layer-close1' //
                    , '{background-position:{{d.layerCloseBtnColor}}px -40px;}' //
                    , '.layui-btn' // 标准按钮 背景颜色 字体颜色 字体
                    , '{background-color:{{d.btnBgInput}};color:{{d.btnFontColorInput}};font-family:{{d.btnFont}} !important;}' //
                    , '.layui-btn i' //
                    , '{color:{{d.btnIconColorInput}} !important;}' //
                    , '.layui-btn.layui-btn-danger' // 警告按钮 背景颜色 字体颜色 字体
                    , '{background-color:{{d.dangerBtnBgInput}};color:{{d.dangerBtnFontColorInput}};font-family:{{d.dangerBtnFont}} !important;}' //
                    , '.layui-btn.layui-btn-danger i' //
                    , '{color:{{d.dangerBtnIconColorInput}} !important;}' //
                    , '.layui-form-switch.layui-form-onswitch' // 开关按钮 背景颜色 字体颜色 字体
                    , '{border-color:{{d.switchBgInput}};background-color:{{d.switchBgInput}};}' //
                    , '.layui-form-switch.layui-form-onswitch em' //
                    , '{color:{{d.switchFontColorInput}} !important;}' //
                    , '.layui-unselect.layui-form-switch em' //
                    , '{font-family:{{d.switchFont}} !important;}' //
                    , '.layui-form-switch.layui-form-onswitch i' //
                    , '{background-color:{{d.switchFontColorInput}};}' //
                    , '.layui-form-checkbox.layui-form-checked' // 复选框-默认 背景颜色 字体颜色 字体
                    , '{border-color:{{d.checkBoxBgInput}};}' //
                    , '.layui-unselect.layui-form-checkbox' //
                    , '{font-family:{{d.checkboxFont}} !important;}' //
                    , '.layui-form-checkbox.layui-form-checked span' //
                    , '{background-color:{{d.checkBoxBgInput}};color:{{d.checkBoxFontColorInput}};}' //
                    , '.layui-form-checkbox.layui-form-checked i' //
                    , '{color:{{d.checkBoxBgInput}};}' //
                    , '.layui-form-checkbox.layui-form-checked[lay-skin="primary"] i' // 复选框-原始 背景颜色 字体颜色
                    , '{border-color:{{d.originCheckBoxBgInput}};background-color:{{d.originCheckBoxBgInput}};color:{{d.originCheckBoxFontColorInput}};}' //
                    , '.layui-form-checkbox.layui-form-checked[lay-skin="primary"] span' //
                    , '{color: #666;background: none!important;}' //
                    , '.layui-form-radio.layui-form-radioed i' //
                    , '{color:{{d.radioBgInput}};}' //
					, '.layui-form-select dl dd.layui-this' //
					, '{background-color:{{d.menuBgInput}};}' //
                    , '.layui-tab-brief>.layui-tab-title .layui-this:after, .layui-tab-brief>.layui-tab-more li.layui-this:after'
                    , '{border-color:{{d.menuBgInput}};}'
                ].join('')).render(data),
                // iframe 父页面主题调整
                parentStyleText = laytpl([
                    //主题色
                    '.layui-side-menu' // 左侧菜单树背景色
                    , '{font-family:{{d.menuFont}} !important;background-color:{{d.menuBgInput}};}' //
                    , '.navbar-side-search' //
                    , '{background-color:{{d.menuBgInput}};}' //
                    , '.navbar-side-search input' //
                    , '{background-color:{{d.searchBoxBgInput}};border-color:{{d.searchBoxBgInput}};color:{{d.searchBoxFontColorInput}};}' //
                    , '.navbar-side-search input:hover' //
                    , '{border-color:{{d.searchBoxBgInput}};}' //
                    , '.navbar-side-search input:focus' //
                    , '{border-color:{{d.searchBoxBgInput}};}' //
                    , '.navbar-side-search input::-webkit-input-placeholder' //
                    , '{color:{{d.searchBoxFontColorInput}};}' //
                    , '.navbar-side-search input::-moz-placeholder' //
                    , '{color:{{d.searchBoxFontColorInput}};}' //
                    , '.navbar-side-search input::-ms-input-placeholder' //
                    , '{color:{{d.searchBoxFontColorInput}};}' //
                    , '.layui-side-menu .layui-nav.layui-nav-tree a,' // 左侧菜单树字体色和图标颜色
                    , '.layui-side-menu .layui-nav.layui-nav-tree i.layui-icon' //
                    , '{color:{{d.menuFontColorInput}} !important;}' //
                    , '.layui-side-menu .layui-nav.layui-nav-tree' //
                    , '{border-top:{{d.menuItemBorderColorInput}};}' //
                    , '.layui-side-menu .layui-nav.layui-nav-tree li.layui-nav-item' //
                    , '{border-bottom:{{d.menuItemBorderColorInput}};}' //
                    , '.layui-side-menu .layui-nav.layui-nav-tree span.layui-nav-more' // 三角图标颜色
                    , '{border-top-color:{{d.menuFontColorInput}} !important;}' //
                    , '.layui-side-menu .layui-nav.layui-nav-tree .layui-nav-itemed a span.layui-nav-more' //
                    , '{border-top-color:{{d.menuBgInput}} !important;}' //
                    , '.layui-side-menu .layui-nav.layui-nav-tree .layui-nav-itemed>.layui-nav-child' //
                    , '{background-color:{{d.childMenuBgInput}} !important;}' //
                    , '.layui-side-menu .layui-nav.layui-nav-tree .layui-this a,' // 左侧菜单树选中项背景色和字色
                    , '.layui-side-menu .layui-nav.layui-nav-tree .layui-nav-bar' //
                    , '{background-color:{{d.menuSelectedBgInput}} !important;color:{{d.menuSelectedFontColorInput}} !important;}' //
                    , '.layui-layout-admin .layui-header' // 顶部导航栏背景色和字体
                    , '{font-family:{{d.navFont}} !important;background-color:{{d.navBgInput}};}' //
                    , '.layui-layout-admin .layui-header a:hover,' //
                    , '.layui-layout-admin .layui-header a cite' // 顶部导航栏字色
                    , '{color:{{d.navFontColorInput}};}' //
                    , '.layui-layout-admin .layui-header a i.layui-icon,' //
                    , '.layui-layout-admin .layui-header a i.zlui-icon,' //
                    , '.layui-layout-admin .layui-header a i.fa' // 顶部导航栏图标颜色
                    , '{color:{{d.navIconColorInput}};}' //
                    , '.layui-layout-admin .layui-header a span.layui-nav-more' // 顶部导航栏三角图标颜色
                    , '{border-top-color:{{d.navIconColorInput}} !important;}' //
                    , '.layui-layout-admin .layui-header .layui-nav .layui-nav-mored' // 顶部导航栏三角图标hover时颜色
                    , '{border-top-color:{{d.navBgInput}} !important;border-bottom-color:{{d.navIconColorInput}} !important;}' //
                    , '.layui-layout-admin .layui-logo' // 左上角logo 背景色和字色
                    , '{background-color:{{d.logoBgInput}};color:{{d.logoFontColorInput}};}' //
                    , '.layadmin-pagetabs .layui-tab-title li:after,' //
                    , '.layadmin-pagetabs .layui-tab-title li.layui-this:after' // pagetabs页签 顶部边框色
                    , '{background-color:{{d.menuBgInput}};}' //
                    , '.layui-tab-brief>.layui-tab-title .layui-this:after, .layui-tab-brief>.layui-tab-more li.layui-this:after'
                    , '{border-color:{{d.menuBgInput}};}'
                    , '.layui-layer-tips.menuTips .layui-layer-content' //
                    , '{background-color:{{d.childMenuTipsBgInput}};color:{{d.menuFontColorInput}};}' //
                    , '.layui-layer-tips.menuTips .layui-layer-content i' //
                    , '{border-bottom-color:{{d.childMenuTipsBgInput}};}' //
                ].join('')).render(data);

            if ('styleSheet' in style) {
                style.setAttribute('type', 'text/css');
                style.styleSheet.cssText = styleText;
            } else {
                style.innerHTML = styleText;
            }
            style.id = id;

            if ('styleSheet' in p_style) {
                p_style.setAttribute('type', 'text/css');
                p_style.styleSheet.cssText = parentStyleText;
            } else {
                p_style.innerHTML = parentStyleText;
            }
            p_style.id = pid;

            styleElem && $('body')[0].removeChild(styleElem);
            p_styleElem && $('body', parent.document)[0].removeChild(p_styleElem);
            $('body')[0].appendChild(style);
            $('body', parent.document)[0].appendChild(p_style);
            $('body').attr('layadmin-themealias', data["theme_title-py"]);
            $('body', parent.document).attr('layadmin-themealias', data["theme_title-py"]);

            var iframeBody = $(document).find('iframe').contents();

            function frameTheme(iframeBody){
                for (var i = 0; i < iframeBody.length; i++) {
                    var c_style = $(iframeBody[i])[0].createElement('style'),
                        c_styleElem = $(iframeBody[i])[0].getElementById(id);
                    if ('styleSheet' in c_style) {
                        c_style.setAttribute('type', 'text/css');
                        c_style.styleSheet.cssText = styleText;
                    } else {
                        c_style.innerHTML = styleText;
                    }
                    c_style.id = id;

                    c_styleElem && $(iframeBody[i]).find("body")[0].removeChild(c_styleElem);
                    $(iframeBody[i]).find("body")[0] && $(iframeBody[i]).find("body")[0].appendChild(c_style);
                    $(iframeBody[i]).find("body").attr('layadmin-themealias', data["theme_title-py"]);

                    if($(iframeBody[i]).find("body")[0] && $(iframeBody[i]).find("body").find("iframe").contents().length > 0){
                        frameTheme($(iframeBody[i]).find("body").find("iframe").contents())
                    }
                }
            }

            setTimeout(function(){
                $('body').addClass('loaded');
                $('#loader-wrapper .load_title').remove();
                frameTheme(iframeBody)
            },50);
        },
        hexify: function (color) {
            var values = color
                .replace(/rgba?\(/, '')
                .replace(/\)/, '')
                .replace(/[\s+]/g, '')
                .split(',');
            var a = parseFloat(values[3] || 1),
                r = Math.floor(a * parseInt(values[0]) + (1 - a) * 255),
                g = Math.floor(a * parseInt(values[1]) + (1 - a) * 255),
                b = Math.floor(a * parseInt(values[2]) + (1 - a) * 255);
            return "#" +
                ("0" + r.toString(16)).slice(-2) +
                ("0" + g.toString(16)).slice(-2) +
                ("0" + b.toString(16)).slice(-2);
        }

    }

    exports('zlTheme', zlTheme);
});